package cn.jingyuan.swan.system.service.impl;

import cn.jingyuan.swan.system.domain.sys.dao.AccountLoginLogDao;
import cn.jingyuan.swan.system.domain.sys.dao.UserAccountDao;
import cn.jingyuan.swan.system.domain.sys.entity.Account;
import cn.jingyuan.swan.system.domain.sys.entity.AccountLoginLog;
import cn.jingyuan.swan.system.enums.SysAccStatusEnum;
import cn.jingyuan.swan.system.enums.SysAccountTypeEnum;
import cn.jingyuan.swan.system.service.AccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    UserAccountDao userAccountDao;

    @Resource
    AccountLoginLogDao accountLoginLogDao;

    @Override
    public Long register(Long userId, String identifier, String password, SysAccountTypeEnum identityType, SysAccStatusEnum status, String registerIp) {
        // FIXME 修复注册账号逻辑

        Date currentTime = new Date();

        Account sysAccount = new Account()
            .setUserId(userId)
            .setIdentifier(identifier)
            .setIdentityType(identityType)
            .setRegisteredIp(registerIp)
            .setAccStatus(status)
            .setCreateTime(currentTime)
            .setUpdateTime(currentTime);

        userAccountDao.insert(sysAccount);

        return sysAccount.getUserId();
    }

    @Override
    public Long loginLog(AccountLoginLog loginLog) {
        // FIXME 修复登录日志逻辑
        if (null == loginLog.getLoginTime()) {
            loginLog.setLoginTime(new Date());
        }

        accountLoginLogDao.insert(loginLog);

        return loginLog.getId();
    }

    @Override
    public void deleteAccount(Long accountId) {
        userAccountDao.delete(accountId);
    }

    @Override
    public Integer updatePassword(Long userId, SysAccountTypeEnum sysAccountType, String password) {
        List<SysAccountTypeEnum> sysAccountTypes = Arrays.asList(sysAccountType);

        return userAccountDao.updatePassword(userId, sysAccountTypes, password);
    }

    @Override
    public Integer updateStatus(Long accountId, SysAccStatusEnum status) {
        Account sysAccount = new Account();
        sysAccount.setAccountId(accountId).setAccStatus(status)
            .setUpdateTime(new Date());


        return userAccountDao.update(sysAccount);
    }

    @Override
    public Account getAccount(Long accountId) {
        return userAccountDao.get(accountId);
    }

    @Override
    public Account getAccount(String account) {
        return userAccountDao.getByAccount(account);
    }

    @Override
    public List<Account> getAccounts(Long userId) {
        return userAccountDao.findByUserId(userId);
    }

    @Override
    public List<Account> getAccounts(List<Long> accountIds) {
        return userAccountDao.gets(accountIds);
    }

    @Override
    public Account getAccount(String account, SysAccountTypeEnum accountType) {
        return userAccountDao.getBy(account, accountType);
    }

}
